package e.odbo.data.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public  class Migration {
    protected String oldVersion;
    protected String version;

    protected List<Table> addTable=new ArrayList<>();
    protected List<String> removeTable=new ArrayList<>();
    //Map<String,Column[]> alterT=new HashMap<>();

    protected Map<String,Column[]> addColumn=new HashMap<>();
    protected Map<String,String[]> removeColumn=new HashMap<>();
    protected Map<String,Column[]> alterColumn=new HashMap<>();

    //Map<String,>
    protected List<TableData> addData=new ArrayList<>();
    protected List<TableData> removeData=new ArrayList<>();
    protected Map<TableData,TableData> updateData=new HashMap<>();

    protected List<TableForeignKey> addForeignKeys=new ArrayList<>();
    protected Map<String,String[]> removeForeignKeys=new HashMap<>();

    protected List<Index>  addIndex=new ArrayList<>();
    protected List<Index>  removeIndex=new ArrayList<>();

    protected String comment;

    public Migration(String oldVersion, String version) {
        this.oldVersion = oldVersion;
        this.version = version;
    }

    public boolean isSupportVersion(String checkVersion){
        return oldVersion.equalsIgnoreCase(checkVersion);
    }

    public String getSupportVersion(){
        return oldVersion;
    }

    public String getCurrentVersion(){
        return version;
    }

    public String getComment(){
        return comment;
    }

    /**
    List<TableForeignKey> addForeignKeys=new ArrayList<>();
    private void addTableOrder(String table){
        if(!tableOrders.contains(table))
            tableOrders.addData(table);
    }*/

    public Migration comment(String memo){
        this.comment=memo;
        return this;
    }

    //table
    public Migration add(Table ...tables){
        for(Table table:tables)
            addTable.add(table);
        return this;
    }

    public Migration removeTable(String ...tables){
        for(String table:tables)
          removeTable.add(table);
        return this;
    }

    //table data
    public Migration addData(TableData ...tableDatas){
        for(TableData tableData:tableDatas)
            this.addData.add(tableData);
        return this;
    }

    public Migration removeData(TableData ...tableDatas){
        for(TableData tableData:tableDatas)
            this.removeData.add(tableData);
        return this;
    }

    //column
    public Migration add(String table,Column ...columns){
        this.addColumn.put(table,columns);
        return this;
    }

    public Migration remveColumn(String table,String ...columns){
        this.removeColumn.put(table,columns);
        return this;
    }

    public Migration aliteColumn(String table,Column ...columns){
        this.alterColumn.put(table,columns);
        return this;
    }


    //index
    public Migration addIndex(Index ...indexs){
        for(Index index:indexs)
         this.addIndex.add(index);
        return this;
    }

    public Migration removeIndex(Index ...indexs){
        for(Index index:indexs)
            this.removeIndex.add(index);
        return this;
    }


    //tableForeignKey
    public Migration addTableForeignKey(TableForeignKey ...foreignKeys){
        for(TableForeignKey foreignKey:foreignKeys)
            this.addForeignKeys.add(foreignKey);
        return this;
    }

    public Migration removeTableForiegnKey(String table,String ...forigenKeys){
        removeForeignKeys.put(table,forigenKeys);
        return this;
    }

   /**
    * 不好看。直接生成在增加
    //data
    public TableData add(String table,String ...columns){
        TableData tableData=TableData.C(table,columns,this);
        this.addData.add(tableData);
        return tableData;
    }

    public TableData remove(String table,String ...columns){
        TableData tableData=TableData.C(table,columns,this);
        this.removeData.add(tableData);
        return tableData;
    }
    **/

    public Migration update(TableData where,TableData value){
        this.updateData.put(where,value);
        return this;
    }


    public String getOldVersion() {
        return oldVersion;
    }

    public String getVersion() {
        return version;
    }

    public List<Table> getAddTable() {
        return addTable;
    }

    public List<String> getRemoveTable() {
        return removeTable;
    }

    public Map<String, Column[]> getAddColumn() {
        return addColumn;
    }

    public Map<String, String[]> getRemoveColumn() {
        return removeColumn;
    }

    public Map<String, Column[]> getAlterColumn() {
        return alterColumn;
    }

    public List<TableData> getAddData() {
        return addData;
    }

    public List<TableData> getRemoveData() {
        return removeData;
    }

    public Map<TableData, TableData> getUpdateData() {
        return updateData;
    }

    public List<TableForeignKey> getAddForeignKeys() {
        return addForeignKeys;
    }

    public Map<String, String[]> getRemoveForeignKeys() {
        return removeForeignKeys;
    }

    public List<Index> getAddIndex() {
        return addIndex;
    }

    public List<Index> getRemoveIndex() {
        return removeIndex;
    }
}
